#include <borzoi.h>
// #include <gwbase/encrypt/ecies.h>
//#include <gwbase/encrypt/aes_helper.h>
#include <gwbase/base/logging.h>
#include <gwbase/base/status.h>
#include <surbasic/EciesHelper.h>
#include <surbasic/AESHelper.h>

void ecies_test() {
  SDBasic::ecc::EciesHelper e("1111111111111111111111111111111111");
  SDBasic::bytearray::ByteArray p1 =
      SDBasic::bytearray::ByteArray::fromRawStr("1111");
  SDBasic::bytearray::ByteArray c1;
  e.Encrypt(p1, c1);
  printf("CIPHER: %s\n", c1.toHexStr().c_str());

  SDBasic::bytearray::ByteArray c2 = SDBasic::bytearray::ByteArray::fromHexStr(
      "308201383082010c3081c706072a8648ce3d02013081bb0201013012020200e906092a86"
      "48ce3d0102030202014a3040041e00000000000000000000000000000000000000000000"
      "0000000000000001041e0066647ede6c332c7f8c0923bb58213b333b20e9ce4281fe115f"
      "7d8f90ad043d0400fac9dfcbac8313bb2139f1bb755fef65bc391f8b36f8f8eb7371fd55"
      "8b01006a08a41903350678e58528bebf8a0beff867a7ca36716f7e01f81052021e010000"
      "00000000000000000000000013e974e72f8a6922031d2603cfe0d7020102034000043d04"
      "014e57daa39c9fedcdac7c362623fa3ff64dafc5820455fdd287c87bcab100bcf0cd3bca"
      "087a79ddda7a71557fb72943b86e19a5be0ccc524cd6d957041065b39ee6aba8993f37a3"
      "6560e4f7363104141ccf9a8228b2ecfb779af52f51a719b87e708f92");

  SDBasic::bytearray::ByteArray p2;
  e.Decrypt(c2, p2);
  printf("PLAIN: %s\n", p2.toRawStr().c_str());
}

int main() {
  ecies_test();
  return 0;

  const char* privkey_cipher = "";
  const char* password = "aA111111";
  SDBasic::bytearray::ByteArray privkey_bin;
  const char* personal_key =
      "MIIBWDCCAQwwgccGByqGSM49AgEwgbsCAQEwEgICAOkGCSqGSM49AQIDAgIBSjBABB4AAAAA"
      "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEEHgBmZH7ebDMsf4wJI7tYITszOyDpzkKB_"
      "hFffY-QrQQ9BAD6yd_LrIMTuyE58bt1X-9lvDkfizb4-Otzcf1ViwEAagikGQM1BnjlhSi-"
      "v4oL7_hnp8o2cW9-AfgQUgIeAQAAAAAAAAAAAAAAAAAAE-l05y-KaSIDHSYDz-"
      "DXAgECA0AABD0EAWVjSreJOA2vomVI9fId6fPWhEKTrCvXTUswWxLtAZEUxPaf1c6Yd8v1Hf"
      "DEDwJlr4CjMk8PGWLY1FMLBDCX59_"
      "y2wFLUkZy0FUOZDUWYBjRUJdkFBXFqWD6w8pdE7xZum2iEdXKZ2Dju7FmcCUEFEudo0qISCa"
      "NrRX5fqlZOoHnb7Xk";

  int ret = SDBasic::ecc::EciesHelper::TryParsePrivKey(
      "pySN8+XnS+bQMmDPEl7toeGra7XH0Qc2eeEuG/TWTpo=", password, &privkey_bin);
  if (ret != 1) {
    LOG_ERROR << "failed to parse private key";
    return -1;
  }

  LOG_INFO << "priv key (hex): " << privkey_bin.toHexStr();

  LOG_INFO << "personal key length: " << strlen(personal_key);
  SDBasic::ecc::EciesHelper e(privkey_bin.toHexStr());

  ByteArray storage_key;
  ret = e.DecryptStorageKeyFromPersonalKey(personal_key, &storage_key);
  if (ret != 1) {
    LOG_ERROR << "failed to decrypt: " << ret;
    return -1;
  } else {
    LOG_INFO << "Success";
  }

  SDBasic::ecc::EciesHelper* e2 =
      SDBasic::ecc::EciesHelper::CreateFromPrivateKeyReturnedByServer(
          "Abhw8tmVgt8ZicAv2ICY1N25iUiO9PxXJQyDrzul");
  ByteArray storage_key2;
  ret = e2->DecryptStorageKeyFromPersonalKey(personal_key, &storage_key2);
  if (ret != 1) {
    LOG_ERROR << "failed to decrypt: " << ret;
    return -1;
  } else {
    LOG_INFO << "Success";
  }
  return 0;
}
